package com.woniu.user.consumer;

import com.rabbitmq.client.Channel;
import com.woniu.common.entity.Cancellation;
import com.woniu.user.mapper.UserMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class UserConsumer{

      @Autowired
      RedisTemplate redisTemplate;
      @Autowired
      UserMapper userMapper;

//    @RabbitListener(queues = "userqueue")
//    //可以使用这三个参数，顺序和个数没有要求
//    //String msg,   消息体
//    // Message message, 消息的完整信息
//    // Channel channel， 信道
//    public void receiveuser(String msg, Message message, Channel channel){
//        System.out.println("userqueue中的msg="+msg);
//    }

    @RabbitListener(queues = "userdelqueue")
    //可以使用这三个参数，顺序和个数没有要求
    //String msg,   消息体
    // Message message, 消息的完整信息
    // Channel channel， 信道
    public void receuveuserdel(String msg, Message message, Channel channel) throws IOException {
        //这个死信是修改注销宠物状态的
        if(redisTemplate.opsForHash().hasKey("cancelDog",""+msg+"")) {
            Cancellation cancellation = (Cancellation) redisTemplate.opsForHash().get("cancelDog", "" + msg + "");
            //将它状态修改为3,就是快要到审核截止日期了,即紧急状态
            userMapper.updateCancelDog(Integer.parseInt(msg));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
